import { Process, Processor } from '@nestjs/bull';
import { Job } from 'bull';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Order } from './order.entity';

@Processor('order')
@Injectable()
export class OrderQueueProcessor {
  constructor(
    @InjectRepository(Order)
    private readonly orderRepository: Repository<Order>,
  ) {}

  @Process('auto-cancel')
  async handleAutoCancel(job: Job<{ orderId: number }>) {
    const order = await this.orderRepository.findOne({ where: { id: job.data.orderId } });
    if (order && order.status === 'pending') {
      order.status = 'cancelled';
      await this.orderRepository.save(order);
    }
  }
} 